package scau.mis4.chanyulin.atm.view;

import javafx.scene.Cursor;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import scau.mis4.chanyulin.atm.APP;
import scau.mis4.chanyulin.atm.base.common.PATH;
import scau.mis4.chanyulin.atm.base.utils.UiUtils;
import scau.mis4.chanyulin.atm.domain.Card;

/**
 * Created by chanyulin on 2018/4/27.
 * 读卡器图形
 */
public class CardReaderView extends ImageView {

    // 图片资源
    public final Image CARD_EMPTY_IMG = UiUtils.getImage(PATH.CARD_READER_EMPTY_IMG_PATH);
    public final Image CARD_READING_IMG = UiUtils.getImage(PATH.CARD_READER_READING_IMG_PATH);
    public final Image CARD_EJECTED_IMG = UiUtils.getImage(PATH.CARD_READER_EJECT_IMG_PATH);

    public CardReaderView() {
        this.setImage(CARD_EMPTY_IMG);
        this.setCursor(Cursor.HAND);
        // 单击触发取卡事件
        this.setOnMouseClicked(e -> {
            takeCardRequest();
        });
    }

    /**
     * 退卡成功后回调此方法
     */
    public void ejectCardSuccessCallBack() {
        this.setImage(CARD_EJECTED_IMG);
    }

    /**
     * 取卡请求
     * @return
     */
    public void takeCardRequest() {
        if (CARD_EJECTED_IMG != getImage()) return;

        Card cardToTake = APP.ATM.takeCardHandler();
        APP.customerBagView.addCard(cardToTake);
        this.setImage(CARD_EMPTY_IMG);
    }

    /**
     * 修改图片为空卡状态
     */
    public void setCardEmptyImg() {
        this.setImage(CARD_EMPTY_IMG);
    }

    /**
     * 修改图片为退卡状态
     */
    public void setCardEjectedImg() {
        this.setImage(CARD_EJECTED_IMG);
    }

}
